// Modules to control application life and create native browser window
const { app, BrowserWindow } = require('electron')
const path = require('node:path')

function createWindow () {
  // 1、创建窗口实例
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js')
    }
  })
  // 2、让窗口去加载index.html文件界面，这个界面就是渲染进程了
  mainWindow.loadFile('index.html')
  // 是否启动打开开发者工具
  mainWindow.webContents.openDevTools()
  // 专门用于监听浏览器窗口中的网页内容加载完成的情况
  mainWindow.webContents.on('did-finish-load', () => {
    console.log('did-finish-load')
  })
  mainWindow.on('closed', function () {
    console.log('closed')
    mainWindow = null
  })
}

// 在 Electron 完成启动和初始化之后 resolve，这时应用已经准备好创建和管理浏览器窗口
app.whenReady().then(() => {
  createWindow()
  // 注册了一个 'activate' 事件监听器，这主要针对 macOS 系统。
  // 当用户点击 Dock 上的应用图标，且没有其他窗口打开时，
  // 这个监听器会触发 createWindow 函数，从而重新创建一个窗口，这是 macOS 应用的一个常见交互习惯
  app.on('activate', function () {
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
  console.log('window-all-closed')
  if (process.platform !== 'darwin') app.quit()
})
app.on('before-quit', function () {
  console.log('quit')
})
app.on('will-quit', function () {
  console.log('will-quit')
})
app.on('quit', function () {
  console.log('quit')
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
